package net.a6polo.a6poloweibo.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.a6polo.a6poloweibo.R;
import net.a6polo.a6poloweibo.sina.domain.Comment;
import net.a6polo.a6poloweibo.sina.domain.Comments;
import net.a6polo.a6poloweibo.util.AsynImageLoader;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentListViewAdapter extends BaseAdapter {

	private Comments comments;
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private Context context;

	public CommentListViewAdapter(Comments comments, Context context) {
		this.comments = comments;
		this.context = context;
	}

	public void addItem(Comments comments) {
		this.comments.getComments().addAll(comments.getComments());
	}

	@Override
	public int getCount() {
		if (null != comments) {
			return comments.getComments().size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		return comments.getComments().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = rowViews.get(position);

		if (null == rowView) {

			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(R.layout.coments_listview_item, null);

			Comment item = (Comment) getItem(position);

			TextView weiboId = (TextView) rowView.findViewById(R.id.coment_id);
			weiboId.setText(item.getId() + "");

			TextView userName = (TextView) rowView.findViewById(R.id.coment_user_name);
			TextView text = (TextView) rowView.findViewById(R.id.coment_text);
			userName.setText(item.getUser().getName());
			text.setText(item.getText());

			TextView date = (TextView) rowView.findViewById(R.id.coment_date);
			Date datetime = new Date(item.getCreated_at());
			SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
			String str = sdf.format(datetime);
			date.setText(str);

			AsynImageLoader loader = new AsynImageLoader();
			ImageView userIcon = (ImageView) rowView.findViewById(R.id.coment_user_head);
			Bitmap userHead = loader.loadDrawableFromNet(userIcon, item.getUser().getProfile_image_url());
			userIcon.setImageBitmap(userHead);

			if (item.getUser().isVerified()) {
				ImageView vUser = (ImageView) rowView.findViewById(R.id.coment_user_v);
				vUser.setVisibility(ImageView.VISIBLE);
			}

			rowViews.put(position, rowView);

			rowView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					// TextView weiboId = (TextView)
					// view.findViewById(R.id.sinaWeiboId);
					// Intent intent = new Intent();
					// intent.putExtra("weiboId", weiboId.getText());
					// intent.setClass(context, WeiboDetailsActivity.class);
					// context.startActivity(intent);

				}

			});
		}

		return rowView;
	}

}
